--[[
 Gets an artwork from DVDINFO

 $Id$
 Copyright © 2007 the VideoLAN team

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
--]]

require "simplexml"

-- Return the artwork
function fetch_art()
	if vlc.item == nil then return nil end

	local meta = vlc.item:metas()
	local dvdID = meta["DVDid"]

	if dvdID then
		-- Movie lookup using DVDINFO:
		-- See http://www.dvdid.info/faq.php#f1r0
		dvdidInfoApi = "http://www.api.dvdid.info/"
		dvdidInfoApiKey = "9069d0e8b554bca3df1592c83c498c5cd0661c2e"
		dvdidInfoApiGetInfoMethod = "getInfo"
		windowsArtBaseURL = "http://services.windowsmedia.com/dvdcover/"
		
		local dvdidInfoURL = dvdidInfoApi..dvdidInfoApiKey.."/"..dvdidInfoApiGetInfoMethod.."/"..vlc.strings.encode_uri_component(dvdID).."/xml"

		vlc.msg.dbg( "### VLC4DVX: dvdidInfoURL for artwork is "..dvdidInfoURL )
		local feed = simplexml.parse_url( dvdidInfoURL )
		for _, dvdidInfo in ipairs( feed.children ) do
			simplexml.add_name_maps( dvdidInfo )
			dvdidInfoArtURL = dvdidInfo.children_map["largeCoverParams"][1].children[1] or dvdidInfo.children_map["smallCoverParams"][1].children[1]

			if dvdidInfoArtURL then 
				vlc.msg.dbg( "### VLC4DVX: artwork URL is "..windowsArtBaseURL..dvdidInfoURL )
				return windowsArtBaseURL..dvdidInfoArtURL
			end
		end
	end

	return nil
end
